#!/usr/bin/python
#
#--------------------------------------------------------------------
# Data Structures
#--------------------------------------------------------------------
# @email: { 
#     xmpp: @gtalk,
#     facebook: @facebook,
#     twitter: @twitter,
#     emails:[@email1, @email2, ...],
#     labels: [@label1, @label2, ...],
# }
#

from Predef import Predef as PRE
from Predef import EmailProperty
from box import Box 
from google.appengine.ext import ndb

class Contact(ndb.Model):
    ############
    # Initialize
    ############
    xmpp = EmailProperty(required=True)
    facebook = EmailProperty()
    twitter = EmailProperty()
    emails = EmailProperty(repeated=True)
    labels = ndb.StringProperty(repeated=True)

    @staticmethod
    def addUser(x):
        if Contact.getCon(x) is None:
            c = Contact(xmpp=x)
            c.put()

    @classmethod
    def getCon(cls, x):
        query = cls.query(cls.xmpp == x)
        return query.get()

'''
from modules.Warehouse import Warehouse 

class namecard:
    ############
    # Initialize
    ############
    def __init__(self,key):
        self.xmpp=PRE.isValidEmail(key)
        self.facebook = self.xmpp
        self.twitter = self.xmpp
        self.emails=list()
        self.labels=list()
        self.emails.append(self.xmpp)
        self.Ware=Warehouse(PRE.INFOS)

    def update(self, d):
        if PRE.xmpp in d:
            self.xmpp=PRE.isValidEmail(d[PRE.xmpp])

        if PRE.facebook in d:
            self.facebook=PRE.isValidEmail(d[PRE.facebook])

        if PRE.twitter in d:
            self.twitter=PRE.isValidEmail(d[PRE.twitter])

        if PRE.emails in d:
            for e in d[PRE.emails]:
                self.emails.append(PRE.isValidEmail(e))

        if PRE.lables in d:
            for e in d[PRE.labels]:
                self.setLabels(e)

        return self
 
    ############
    # scatter & gather
    ############
    def updatetwitter(self,x):
        e = PRE.isValidEmail(x)
        if e is not None:
            self.twitter=e

    def updatefacebook(self,x):
        e = PRE.isValidEmail(x)
        if e is not None:
            self.facebook=e

    def addEmail(self,x):
        e = PRE.isValidEmail(x)
        if (e is not None) and (e not in self.emails):
            self.emails.append(e)

    def delEmail(self,e):
        if e in self.emails:
            self.emails.remove(e)

    def addLabel(self,l):
        if label not in self.Ware.List():
            self.labels.append(l) 

    def delLabel(self,l):
        if l in self.labels:
            self.labels.remove(l)

    def getLabel(self):
        return self.labels

    ############
    # process
    ############
    def export(self):
        # 'xmpp':@XMPP
        x = "'{}'".format("':'".join([PRE.xmpp, self.xmpp]))
        # 'facebook':@facebook
        f = "'{}'".format("':'".join([PRE.facebook, self.facebook]))
        # 'twitter':@twitter
        t = "'{}'".format("':'".join([PRE.twitter, self.twitter]))
        # 'emails':[@email1,@email2,...]
        e = "'{}':['{}']".format(PRE.emails, "','".join(self.emails))
        # 'lables':[@label1,@2,...]
        l = "'{}':['{}']".format(PRE.labels, "','".join(self.labels))

        # contact
        return "{"+",".join([x,f,t,e,l])+"}"
'''

